package com.lly.songhigh.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.MusicInfo;

import java.util.List;

/**
 * Created by 李新伟 on 15/11/4.
 * 音乐播放列表适配器
 */
public class MusicListAdapter extends BaseAdapter {
    private List<MusicInfo> musicInfos;

    public MusicListAdapter(List<MusicInfo> musicInfos) {
        this.musicInfos = musicInfos;
    }

    @Override
    public int getCount() {
        return musicInfos != null && musicInfos.size() > 0 ? musicInfos.size() : 0;
    }

    @Override
    public MusicInfo getItem(int position) {
        return musicInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.music_list_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.position = (TextView) convertView.findViewById(R.id.music_list_item_position);
            viewHolder.musicItemSong = (TextView) convertView.findViewById(R.id.music_item_songName);
            viewHolder.musicItemSinger = (TextView) convertView.findViewById(R.id.music_item_singerName);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MusicInfo musicInfo = getItem(position);
        viewHolder.position.setText(position + 1 + "");
        viewHolder.musicItemSong.setText(musicInfo.getSongName());
        viewHolder.musicItemSinger.setText(musicInfo.getSingerName());
        return convertView;
    }

    public class ViewHolder {
        private TextView position;
        private TextView musicItemSong;
        private TextView musicItemSinger;
    }
}
